package io.techery.properratingbarsample.slice;

import io.techery.properratingbar.ProperRatingBar;
import io.techery.properratingbar.RatingListener;
import io.techery.properratingbarsample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    private ProperRatingBar upperRatingBar;
    private ProperRatingBar lowerRatingBar;
    private Button toggleClicksEnabledButton;
    private String[] ticksArray = new String[]{"$", "★"};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取 upperRatingBar 自定义组件
        upperRatingBar = (ProperRatingBar) findComponentById(ResourceTable.Id_upperRatingBar);

        // 获取 lowerRatingBar 自定义组件
        lowerRatingBar = (ProperRatingBar) findComponentById(ResourceTable.Id_lowerRatingBar);
        lowerRatingBar.setListener(ratingListener);

        // toggleTick 点击事件
        Button btntoggleTick = (Button) findComponentById(ResourceTable.Id_toggleTick);
        btntoggleTick.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //点击 switch tick
                toggleTick();
            }
        });

        // get Rating 点击事件
        Button btnGet = (Button) findComponentById(ResourceTable.Id_getRatingButton);
        btnGet.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //点击 get Rating
                getRatingClicked();
            }
        });

        // setRating 点击事件
        Button btnSet = (Button) findComponentById(ResourceTable.Id_setRatingButton);
        btnSet.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //点击 set Rating
                setRatingClicked();
            }
        });

        // 点击 toggle Enabled
        toggleClicksEnabledButton = (Button) findComponentById(ResourceTable.Id_toggleClicksEnabledButton);
        toggleClicksEnabledButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //点击 enable
                toggleClicksEnabled();
            }
        });
    }

    /**
     * 点击 switch tick
     */
    void toggleTick() { // here goes shit-code. No time to do properly
        String tick;
        if (upperRatingBar.getSymbolicTick().equals(ticksArray[0])) {
            tick = ticksArray[1];
        } else {
            tick = ticksArray[0];
        }
        upperRatingBar.setSymbolicTick(tick);
    }

    /**
     * 点击 get Rating
     */
    void getRatingClicked() {
        //底部弹出信息
        new ToastDialog(getContext())
                .setText(String.format(getString(ResourceTable.String_get_rating_snack_caption_format), lowerRatingBar.getRating()))
                .show();
    }

    /**
     * 点击 set Rating
     */
    void setRatingClicked() {
        lowerRatingBar.setRating(2);
        //底部弹出信息 提示
        new ToastDialog(getContext())
                .setText(getString(ResourceTable.String_set_rating_snack_caption))
                .show();
    }

    /**
     * 点击 disable/enable 调用
     */
    void toggleClicksEnabled() {
        lowerRatingBar.toggleClickable();
        toggleClicksEnabledButton.setText(lowerRatingBar.isClickable() ?
                ResourceTable.String_disable_clicks_button_caption : ResourceTable.String_enable_clicks_button_caption);

        // 设置弹出text
        String clicTtext = getString(lowerRatingBar.isClickable() ? ResourceTable.String_enabled_clicks_snack_caption :
                ResourceTable.String_disabled_clicks_snack_caption);
        // 弹出点击提示
        new ToastDialog(getContext())
                .setText(clicTtext)
                .show();
    }


    private RatingListener ratingListener = new RatingListener() {
        @Override
        public void onRatePicked(ProperRatingBar ratingBar) {
            new ToastDialog(getContext())
                    .setText(String.format(getString(ResourceTable.String_rating_listener_snack_caption), ratingBar.getRating()))
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .show();
        }
    };
}
